package com.droidclaimcustomer.dal;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

public class DriodClaimCustomerDal extends ContentProvider {
	public static final String AUTHORITY = "com.droidclaim.SurveyorContentProvider";
	private static final int ALL_ROWS = 1;
	private static final int SINGLE_ROW = 2;
	
	public static final String CLAIMINFO_PATH = "claiminfo";
	public static final Uri CLAIMINFO_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + CLAIMINFO_PATH);

	
	/*public static final String KEY_ID 				= ClaimInfoDB.KEY_ID;
	public static final String KEY_ACC_DATE 		= ClaimInfoDB.KEY_ACC_DATE;
	public static final String KEY_ACC_LOCATION 	= ClaimInfoDB.KEY_ACC_LOCATION;
	public static final String KEY_ACC_LATITUDE 	= ClaimInfoDB.KEY_ACC_LATITUDE;
	public static final String KEY_ACC_LONGITUDE 	= ClaimInfoDB.KEY_ACC_LONGITUDE;
	public static final String KEY_UPDATE_DATE 		= ClaimInfoDB.KEY_UPDATE_DATE;
	public static final String KEY_STATUS 			= ClaimInfoDB.KEY_STATUS;
	public static final String KEY_LICENSEPLATENO 	= ClaimInfoDB.KEY_LICENSEPLATENO;
	public static final String KEY_SURVNAME 		= ClaimInfoDB.KEY_SURVNAME;
	public static final String KEY_SURVMOBILE 		= ClaimInfoDB.KEY_SURVMOBILE;
	public static final String KEY_IMG_PATH 		= ClaimInfoDB.KEY_IMG_PATH;*/

	
	private static final UriMatcher uriMatcher;
	private ClaimInfoDBHelper claimInfoDB;

	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(AUTHORITY, CLAIMINFO_PATH, ALL_ROWS);
		uriMatcher.addURI(AUTHORITY, CLAIMINFO_PATH + "/#", SINGLE_ROW);
	}

	@Override
	public boolean onCreate() {
		claimInfoDB = new ClaimInfoDBHelper(getContext(),
				ClaimInfoDBHelper.DATABASE_NAME, null,
				ClaimInfoDBHelper.DATABASE_VERSION);
		return true;
	}
	
	@Override
	public String getType(Uri uri) {
		String result = null;
		try {
			// Return a string that identifies the MIME type
			// for a Content Provider URI
			switch (uriMatcher.match(uri)) {
			case ALL_ROWS:
				result = "vnd.android.cursor.dir/vnd.droidclaim.SurveyorContentProvider";
				break;
			case SINGLE_ROW:
				result = "vnd.android.cursor.item/vnd.droidclaim.SurveyorContentProvider";
				break;
			default:
				throw new IllegalArgumentException("Unsupported URI: " + uri);

			}
		} catch (Exception ex) {
			result = null;
		}

		return result;
	}
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = claimInfoDB.getWritableDatabase();
		
		// If this is a row URI, limit the deletion to the specified row.
		switch (uriMatcher.match(uri)) {
			case SINGLE_ROW:
				String rowIdStr = uri.getPathSegments().get(1);
				String inputSelectStr = "";
				if (!TextUtils.isEmpty(selection)) {
					inputSelectStr = " AND (" + selection + ')';
				}
				selection = ClaimInfoDBHelper.KEY_ID + "=" + rowIdStr + inputSelectStr;
				break;
		}

		// To return the number of deleted items you must specify a where
		// clause. To delete all rows and return a value pass in "1".
		if (selection == null) {
			selection = "1";
		}
		// Perform the deletion.
		int deleteCount = db.delete(ClaimInfoDBHelper.DATABASE_TABLE,
				selection, selectionArgs);
		// Notify any observers of the change in the data set.
		getContext().getContentResolver().notifyChange(uri, null);
		// Return the number of deleted items.
		return deleteCount;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Uri insertUri = null;

		SQLiteDatabase db = claimInfoDB.getWritableDatabase();

		// Insert the values into the table
		String nullColumn = null;
		long id = db.insert(ClaimInfoDBHelper.DATABASE_TABLE, nullColumn,
				values);
		if (id > -1) {
			insertUri = ContentUris.withAppendedId(CLAIMINFO_CONTENT_URI, id);
			getContext().getContentResolver().notifyChange(insertUri, null);
		}
		return insertUri;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

		SQLiteDatabase db;
		try {
			db = claimInfoDB.getWritableDatabase();
		} catch (SQLiteException ex) {
			db = claimInfoDB.getReadableDatabase();
		}

		String groupBy = null;
		String having = null;
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

		switch (uriMatcher.match(uri)) {
			case SINGLE_ROW:
				String rowID = uri.getPathSegments().get(1);
				queryBuilder.appendWhere(ClaimInfoDBHelper.KEY_ID + "=" + rowID);
				break;				
		}
		
		queryBuilder.setTables(ClaimInfoDBHelper.DATABASE_TABLE);
		
		Cursor cursor = queryBuilder.query(db, projection, selection,
				selectionArgs, groupBy, having, sortOrder);
		
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = claimInfoDB.getWritableDatabase();

		switch (uriMatcher.match(uri)) {
		case SINGLE_ROW:
			String rowID = uri.getPathSegments().get(1);
			String inputSelectStr = "";
			if (!TextUtils.isEmpty(selection)) {
				inputSelectStr = " AND (" + selection + ')';
			}
			selection = ClaimInfoDBHelper.KEY_ID + "=" + rowID + inputSelectStr;
			break;
		}

		int updateCount = db.update(ClaimInfoDBHelper.DATABASE_TABLE, values,
				selection, selectionArgs);
		getContext().getContentResolver().notifyChange(uri, null);
		return updateCount;
	}
}

